/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ResultEditor.SpanEdit;


import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.text.JTextComponent;

/**
 *
 * @author leng
 */
public class AnnotationClassChooser{
    protected static JPopupMenu popup;
    protected static UserInterface.GUI eHOST_GUI;
    protected static JTextComponent comp;
    protected static String currentAnnotationClass;


    /**class constructor*/
    public AnnotationClassChooser(JTextComponent comp, 
            UserInterface.GUI eHOST_GUI, String currentAnnotationClass){
        AnnotationClassChooser.comp = comp;
        AnnotationClassChooser.eHOST_GUI = eHOST_GUI;
        AnnotationClassChooser.currentAnnotationClass = currentAnnotationClass;
    }
    public AnnotationClassChooser(){
    }

    public void popupDialog(String annotation_text){

        ResultEditor.AnnotationClasses.Depot depot = new ResultEditor.AnnotationClasses.Depot();
        String[] annotationClassnames =  depot.getAnnotationClasssnamesString();
        int size = annotationClassnames.length;


        // popupDialog the popmenu
        popup = new JPopupMenu();
        
        
        for(int i=0; i<size; i++){
            popup = buildpopmenu(popup, annotationClassnames[i], annotation_text);
        }
        if ( size > 0){
            JSeparator seperator = new JSeparator();
            popup.add(seperator);
        }
        popup = buildpopmenu(popup, "Build a new annotation category", annotation_text);




        popup.show(comp, 1, 1);
    }

    private JPopupMenu buildpopmenu(final JPopupMenu popup, final String annotationClassname, final String annotation_text){
        final ActionListener menuListener = new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                SaveAnnotationClassChange( annotationClassname, annotation_text );
            }
        };


        JMenuItem item;
        popup.add(item = new JMenuItem(annotationClassname, new ImageIcon("1.gif")));
        item.setHorizontalTextPosition(JMenuItem.RIGHT);
        item.addActionListener(menuListener);
        if(annotationClassname.compareTo("Build a new annotated class")==0){
            item.setBackground(Color.LIGHT_GRAY);
        }else if(currentAnnotationClass.compareTo(annotationClassname.trim())==0){
            item.setBackground(Color.LIGHT_GRAY);            
        }


        popup.setLabel("Justification");
        popup.setBorder(new BevelBorder(BevelBorder.RAISED));
        return popup;
    }

    /**Save changes into memory if new annotation class designated in the
     * popupmenu.*/
    private void SaveAnnotationClassChange(final String annotationClassname, String annotation_text ){

        if( annotationClassname ==  null )
            return;

        if( annotationClassname.compareTo("Let me build a new annotation class") ==  0 ){
            ResultEditor.NewClass.Creator creator = new ResultEditor.NewClass.Creator(eHOST_GUI);
            creator.setVisible(true);
            return;
        }

        if( annotationClassname.compareTo(AnnotationClassChooser.currentAnnotationClass) ==  0 )
            return;

        // display changes on screen
        ((JTextField)comp).setText(annotationClassname);

        // save changes
        save(annotationClassname, annotation_text);
    }

    public void applyJustBuiltAnnotatedClass(String annotationclass, String annotation_text){
        save(annotationclass, annotation_text);
    }
    
    private void save(String annotationclass, String annotation_text){

        ResultEditor.WorkSpace.WorkSet.currentAnnotation.annotationclass = annotationclass;
        ResultEditor.WorkSpace.WorkSet.currentAnnotation.creationDate = commons.OS.getCurrentDate();

        // after saved changes into memory
        eHOST_GUI.repaintHighlighter();
        eHOST_GUI.showAnnotationCategoriesInTreeView_selectCategory( annotationclass, annotation_text );
    }
    

}

